<form name="frm" action="post">
<?php 
include  "../include/connect.inc.php";
include "../include/config.inc.php";
if (isset($_REQUEST['lang']))
{
	$lang = $_REQUEST['lang'];
}else{
	$lang = "th";
}

if(isset($_REQUEST['ref']))
{
	if (($_REQUEST['ref']!="W") AND ($_REQUEST['ref']!="E") AND ($_REQUEST['ref']!="H"))
	{
		$ref = "W";
	}else{
		$ref = $_REQUEST['ref'];
	}
}else{
	exit;
}

if (isset($_REQUEST['argID']))
{
	$cardid = $_REQUEST['argID'];
	echo generateDDL($cardid);
	echo "<p>".listPicture($cardid)."</p>";
}else{
	$msql = "SELECT  cardid from mas_card  order by cardid limit 1";
	$result = mysql_db_query($dbname,$msql);
	while($rs = mysql_fetch_array($result))
	{
		$cardid = $rs['cardid'];
	}
	mysql_free_result($result);
	echo generateDDL($cardid);
	echo "<p>".listPicture($cardid)."</p>";
}
?>
</form>


<?php

function generateDDL($cardid)
{
	global $dbname;
	global $lang;
	$sql = "select * from mas_card order by cardid";
	$result = mysql_db_query($dbname,$sql);
	$mymessage = "<select name=drpCate onchange='clickChange(document.frm)'>";
	while($rs = mysql_fetch_array($result))
	{
		if ($lang == "th")
		{
			$card_name = $rs['card_name'];
		}else{
			$card_name = $rs['card_name_en'];
		}

		if ($cardid == $rs['cardid'])
		{
			$mymessage .= "<option value='$cardid' selected>".$card_name."</option>";
		}else{
			$mymessage .= "<option value='".$rs['cardid']."'>".$card_name."</option>";
		}
	}
	$mymessage .= "</select>";
	mysql_free_result($result);
	return $mymessage;
}


function listPicture($cardid)
{
	global $dbname;
	global $LINK_CARD_PATH;
	$mms = "";
	$sql = "select  *  from txn_card_picture where cardid=".$cardid;
	$result = mysql_db_query($dbname,$sql) or die("error in list pic");
	while($rs = mysql_fetch_array($result))
	{
		$mms .= " <a href='javascript:void(0);' onclick=add('".$rs['design_thumb']."')><img src=".$LINK_CARD_PATH."/".$rs['design_thumb']." border='0' id='".$rs['design_thumb']."' width='80' height='60' hspace=2></a> ";
	}
	return $mms;
}

?>

<SCRIPT LANGUAGE="JavaScript">
<!--

function clickChange(frm)
{
	var a = frm.drpCate.options[frm.drpCate.selectedIndex].value;
	location.href = "card_category.inc.php?lang=<?php echo $lang?>&ref=<?php echo $ref?>&argID="+a;
}
/*
function add(picture)
{
	var text_add1 = ","+picture;
	var text_add2 = picture;
	var typeroom = '<?php echo $ref;?>';
	var a = top.document.frm.want.value;
	var aa = top.document.frm.exchange.value;
	var b = a.split(',')
	var bb = aa.split(',')

	if (typeroom == 'W'){isWant = true;}else{isWant=false}
	if (document.getElementById(picture).style.filter == 'Gray') {
		document.getElementById(picture).style.filter='';
		document.getElementById(picture).style.cursor='';
					if(isWant)
					{
							a = a.replace(picture,"");
							top.document.frm.want.value = newArrange(a);
					}else{						
							aa = aa.replace(picture,"");
							top.document.frm.exchange.value = newArrange(aa);
					}
	}else {
		//add picture
		document.getElementById(picture).style.filter='Gray';
		document.getElementById(picture).style.cursor='arrow';
					if(isWant)
					{
							if ((a!="") && (b.length>0))
							{
								c = delPic(a,picture,'');
								top.document.frm.want.value  = newArrange(c + text_add1);
							}else{
								c = delPic(a,picture,'');
								top.document.frm.want.value  = newArrange(c + text_add2);
							}
					}else{
							if((aa!="") && (bb.length>0))
							{
								c = delPic(aa,picture);
								top.document.frm.exchange.value  = newArrange(c + text_add1);
							}else{
								c = delPic(aa,picture);
								top.document.frm.exchange.value  = newArrange(c + text_add2);
							}
					}
	}
}*/

function add(picture)
{
	var text_add1 = ","+picture;
	var text_add2 = picture;
	var typeroom = '<?php echo $ref;?>';
	var a = top.document.frm.want.value;
	var aa = top.document.frm.exchange.value;
	var aaa = top.document.frm.have.value;
	var b = a.split(',')
	var bb = aa.split(',')
	var bbb = aaa.split(',')

	if (typeroom == 'W'){isWant = true;}else{isWant=false}


	if (document.getElementById(picture).style.filter == 'Gray') {
		document.getElementById(picture).style.filter='';
		document.getElementById(picture).style.cursor='';
					if(typeroom=='W')
					{
							a = a.replace(picture,"");
							top.document.frm.want.value = newArrange(a);
					}
					if(typeroom=='E')
					{
							aa = aa.replace(picture,"");
							top.document.frm.exchange.value = newArrange(aa);
					}
					if(typeroom=='H')
					{
							aaa = aaa.replace(picture,"");
							top.document.frm.have.value = newArrange(aaa);
					}

	}else {
		//add picture
		document.getElementById(picture).style.filter='Gray';
		document.getElementById(picture).style.cursor='arrow';
					if(typeroom=='W')
					{
							if ((a!="") && (b.length>0))
							{
								c = delPic(a,picture,'');
								top.document.frm.want.value  = newArrange(c + text_add1);
							}else{
								c = delPic(a,picture,'');
								top.document.frm.want.value  = newArrange(c + text_add2);
							}
					}
					if(typeroom=='E'){
							if((aa!="") && (bb.length>0))
							{
								c = delPic(aa,picture);
								top.document.frm.exchange.value  = newArrange(c + text_add1);
							}else{
								c = delPic(aa,picture);
								top.document.frm.exchange.value  = newArrange(c + text_add2);
							}
					}
					if(typeroom=='H'){
							if((aaa!="") && (bbb.length>0))
							{
								c = delPic(aaa,picture);
								top.document.frm.have.value  = newArrange(c + text_add1);
							}else{
								c = delPic(aa,picture);
								top.document.frm.have.value  = newArrange(c + text_add2);
							}
					}
	}



}

function delPic(strBig,strReplace)
{
			return strBig.replace(strReplace,'');
}

function delComma(str)
{
		var comma = ",,";
		var re = new RegExp(comma, 'gi');
		return str.replace(re, ',');
}

function newArrange(str)
{
	var myArray = str.split(',')
	var newString = "";
	for(i=0;i<myArray.length;i++)
	{
		if(myArray[i] !="")
		{
					if(newString == "")
					{
						newString = newString + myArray[i];
					}else{
						newString += ',' + myArray[i];
					}
		}
	}
	return newString;
}
//-->
</SCRIPT>